package com.bbms.controller.bookManage;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbms.entity.ErrorCode;
import com.bbms.entity.Page;
import com.bbms.entity.Publisher;
import com.bbms.entity.Result;
import com.bbms.entity.SuccessCode;
import com.bbms.service.PublisherService;

/**
 * @author Administrator
 * 出版社控制器
 */
@Controller
@RequestMapping("/bbms/bookManage/publisher")
public class PublisherController {
	
	@Resource(name="PublisherServiceImpl")
	private PublisherService pService;
	
	/**
	 * 获取分页数据
	 * @param page 当前页
	 * @param rows 行数
	 * @return 分页数据
	 */
	@ResponseBody
	@RequestMapping(value="list", method=RequestMethod.POST)
	public Page getPage(Integer page, Integer rows){
		Page pageData = pService.getPage(page, rows);
		return pageData;
	}
	
	
	/**
	 * 获得所有数据
	 * @return 所有数据
	 */
	@ResponseBody
	@RequestMapping(value="combox", method=RequestMethod.POST)
	public Object getAllData(){
		Object o = pService.getAllData();
		return o;
	}
	
	/**
	 * 新增出版社
	 * @param publisher 出版社对象
	 * @return 结果信息
	 */
	@ResponseBody
	@RequestMapping(value="add", method=RequestMethod.POST)
	public Result insert(@ModelAttribute(name="publisher") Publisher publisher){
		Result r = new Result();
		try{
			pService.add(publisher);
			r.setStatus(SuccessCode.insertSuccess.getCode());
			r.setMsg(SuccessCode.insertSuccess.getName());
		} catch (Exception e) {
			e.printStackTrace();
			r.setStatus(ErrorCode.insertFailed.getCode());
			r.setMsg(ErrorCode.insertFailed.getName());
		}
		
		return r;
	}
	
	/**
	 * 修改出版社
	 * @param publisher 出版社对象
	 * @return 结果信息
	 */
	@ResponseBody
	@RequestMapping(value="modify", method=RequestMethod.POST)
	public Result modify(@ModelAttribute(name="publisher") Publisher publisher){
		Result r = new Result();
		try{
			pService.update(publisher);
			r.setStatus(SuccessCode.updateSuccess.getCode());
			r.setMsg(SuccessCode.updateSuccess.getName());
		} catch (Exception e) {
			e.printStackTrace();
			r.setStatus(ErrorCode.updateFailed.getCode());
			r.setMsg(ErrorCode.updateFailed.getName());
		}
		
		return r;
	}
	
	/**
	 * 删除出版社
	 * @param publisher 出版社对象
	 * @return 结果信息
	 */
	@ResponseBody
	@RequestMapping(value="del", method=RequestMethod.POST)
	public Result del(@ModelAttribute(name="publisher") Publisher publisher){
		Result r = new Result();
		try{
			pService.delete(publisher);
			r.setStatus(SuccessCode.deleteSuccess.getCode());
			r.setMsg(SuccessCode.deleteSuccess.getName());
		} catch (Exception e) {
			e.printStackTrace();
			r.setStatus(ErrorCode.deleteFailed.getCode());
			r.setMsg(ErrorCode.deleteFailed.getName());
		}
		
		return r;
	}
}
